﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace mstdump_Analyzer
{
    class SwitchConfigurations: IDisposable
    {
        private bool KEEP_RUNNING; 
        private string OUTPUT_DIR_PATH;
        DataTable switchConfigurationsTable = null;
        //
        //----------------------------------------------------
        //
        public SwitchConfigurations(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
            // create a DataTable
            switchConfigurationsTable = new DataTable();
            // add columns
            switchConfigurationsTable.Columns.Add("Attribute", typeof(string));
            switchConfigurationsTable.Columns.Add("Value", typeof(string));
        }
        //
        //----------------------------------------------------
        //
        public DataTable getSwitchConfigurations()
        {
            return switchConfigurationsTable;
        }//
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "active.txt");
            if (path != null)
            {
                scanSwitchConfigurations(path);
            }
        }
        //
        //----------------------------------------------------
        //
        public void scanSwitchConfigurations(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                Item tmpItem = new Item();
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    // read line
                    string line = sr.ReadLine();
                    try
                    {
                        tmpItem = new Item();
                        // if there is only one "Value:" in the line
                        if (Regex.Matches(line, "Value:").Count == 1)
                        {
                            // Attribute
                            tmpItem.itemName = Regex.Split(line, " ")[1];
                            // Value
                            tmpItem.content = Regex.Split(line, "Value:")[1];
                            // add it to the list
                            switchConfigurationsTable.Rows.Add(tmpItem.itemName, tmpItem.content);
                        }
                    }//try
                    catch
                    { }
                }
            }//try
            catch
            { }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //

        public void Dispose()
        {
            if (switchConfigurationsTable != null)
            {
                switchConfigurationsTable.Dispose();
            }
        }
    }
}
